package com.zhiyou100.service;


import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou100.dao.DiscussDao;
import com.zhiyou100.dao.ProjectDao;
import com.zhiyou100.model.Discuss;
import com.zhiyou100.vo.PageVo;

@Service
public class DiscussServiceImpl implements DiscussService{

	@Autowired
	private DiscussDao discussDao;
	@Autowired
	private ProjectDao projectDao;
	
	
	@Override
	public PageVo<Discuss> getAllDiscussByPage(Integer projectId, Integer page, Integer size) {
		int dataCount =discussDao.discussCount(projectId);
		if(dataCount ==0){
			//没有数据
			return new PageVo<>(1, size, 0, 0, null);
			
		}else{
			//计算总页数
			int pageCount =(dataCount+size -1)/size;
			
			//修正pageIndex
			if(page <1){
				page =1;
			}else if(page > pageCount){
				
				page =pageCount;
			}
			//查询数据
			ArrayList<Discuss> discusses =discussDao.getAllDiscussByPage(projectId, (page-1)*size, size);
			return new PageVo<Discuss>(page, size, pageCount, dataCount, discusses);
		}
		
		
		
	}

	@Override
	public void addDiscussByPidAndUid(Discuss discuss) {
		
		discussDao.addDiscussByPidAndUid(discuss);
		
		projectDao.updateDiscussCount(1);
		
	}
	

}
